/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.all;

import dao.NguoidungDAO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import pojo.Nguoidung;

/**
 *
 * @author BlueZky
 */
public class dangNhapController extends AbstractController {

    public dangNhapController() {
    }

    @Override
    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) {
        HttpSession session = request.getSession();
        String action = request.getParameter("a");
        Nguoidung nguoidung = (Nguoidung) session.getAttribute("nguoiDung");

        try {
            if (action == null || action.compareToIgnoreCase("li") == 0) {

                // Kiem tra nguoi dung da dang nhap chua
                if (nguoidung != null) {
                    return new ModelAndView("All/index");
                } else {

                    // Neu chua dang nhap thi lay thong tin dang nhap
                    String tenDangNhap = request.getParameter("userName");
                    String matKhau = request.getParameter("password");

                    //Kiem tra thong tin co hop le hay khong
                    if (tenDangNhap == null || tenDangNhap.length() == 0
                            || matKhau == null || matKhau.length() == 0) {

                        ModelAndView mv = new ModelAndView("All/dangnhap");
                        return mv;
                    } else {

                        //Neu thong tin hop le thi kiem tra co trong csdl khong
                        nguoidung = NguoidungDAO.doLogIn(tenDangNhap, matKhau);

                        if (nguoidung == null) {
                            ModelAndView mv = new ModelAndView("All/dangnhap");
                            mv.addObject("message", "Sai tên đăng nhập hoặc mật khẩu");
                            return mv;
                        } else {
                            session.setAttribute("nguoiDung", nguoidung);
                            if (NguoidungDAO.laAdmin(nguoidung.getTendangnhap()) || NguoidungDAO.laQuanLy(nguoidung.getTendangnhap())) {
                                session.setAttribute("quyen", "true");
                            }
                            response.sendRedirect("trangchu.htm");
                            return null;
                        }
                    }
                }
            } else if (action.compareToIgnoreCase("lo") == 0) {
                session.removeAttribute("quyen");
                session.removeAttribute("nguoiDung");
                response.sendRedirect("trangchu.htm");
                return null;
            } else {
                return new ModelAndView("All/index");
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return null;
        }
    }
}
